package com.example.hao.contentprovider_demo;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;

public class MyContentProvider extends ContentProvider {

    public static final String TAG ="MyContentProvider";
    /**
     * Uri 匹配管理对象
     */
    private static final UriMatcher UM = new UriMatcher(UriMatcher.NO_MATCH);


    static {

        //content://com.xykj.student/students
        UM.addURI("com.xykj.student","students",1);

    }

    private MySQL mySQL;

    public MyContentProvider() {
    }

    /**
     * 创建内容提供者,该方法被调用
     *
     * @return true 表示内容提供者可以使用
     */
    @Override
    public boolean onCreate() {
        Log.d(TAG, "onCreate: 内容提供者已创建");
        mySQL = new MySQL(getContext());
        Log.d(TAG, "onCreate: "+mySQL);
        return true;//表示创建该内容提供者
    }

    /**
     * 查询
     *
     * @param uri
     * @param projection
     * @param selection
     * @param selectionArgs
     * @param sortOrder
     * @return
     */
    @Override
    public Cursor query(@NonNull Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Log.d(TAG, "query: " + uri);
        int match = UM.match(uri);
        switch (match) {
            case 1:
                SQLiteDatabase db = mySQL.getReadableDatabase();
                return db.query(MySQL.TABLE_NAME, projection, selection, selectionArgs, sortOrder, null, null);
            case 2:
//                SQLiteDatabase db = mySqlOpenHelper.getReadableDatabase();
//                return db.query("teacher表", projection, selection, selectionArgs, sortOrder, null, null);
        }
        return null;
    }
    /**
     * 删除
     *
     * @param uri
     * @param selection
     * @param selectionArgs
     * @return
     */
    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
        Log.d(TAG, "delete: "+uri);
        SQLiteDatabase db = mySQL.getReadableDatabase();
        int delete = db.delete(MySQL.TABLE_NAME, selection, selectionArgs);
        db.close();
        return delete;
    }

    /**
     * 插入
     *
     * @param uri
     * @param values
     * @return
     */
    @Override
    public Uri insert(@NonNull Uri uri, ContentValues values) {

        SQLiteDatabase db = mySQL.getReadableDatabase();
        long insert = db.insert(MySQL.TABLE_NAME, null, values);
        Log.d(TAG, "insert: " + uri);
        db.close();

        //content://com.xykj.student.contentprovider/students

        return ContentUris.withAppendedId(uri, insert);
        //content://com.xykj.student.contentprovider/students/5
    }



    /**
     * 更新
     *
     * @param uri
     * @param values
     * @param selection
     * @param selectionArgs
     * @return
     */
    @Override
    public int update(@NonNull Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        Log.d(TAG, "update: " + uri);
        SQLiteDatabase db = mySQL.getReadableDatabase();

        int update = db.update(MySQL.TABLE_NAME, values, selection, selectionArgs);

        Log.d(TAG, "insert: " + uri);

        db.close();
        return update;
    }

    /**
     * 获取 uri 的类型
     *
     * @param uri
     * @return
     */
    @Override
    public String getType(@NonNull Uri uri) {

        return null;
    }
}
